package com.coffeeloversupportlibrary.coffeelover.uicompont;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * @author puyv
 * @createTime 2021/11/8 18:52
 * @projectName CoffeeLoverSupportLibrary
 * @className RadarMapView.java
 * @description TODO
 */
public class RadarMapView extends View {
    private int dataCount = 5;
    private RadarMapData radarMapData;
    private float angle = (float)(Math.PI*2/dataCount);
    private int count = 4;
    private int contextX, contextY;
    private float radius=50;
    private float curR;
    private Paint mainPaint; //雷达区画笔
    private Paint valuePaint; //数据区画笔
    private Paint textPaint;  //文本画笔
    private String[] titles={"甜","b","c","d","e","f"};
    private Double[] values={25.0,67.8,55.4,89.0,56.9,70.0};
    private Double[] temValuses={0.0,0.0,0.0,0.0,0.0,0.0};
    private float maxValue=100;//数据最大值
    private float mDuration=1000;//动画时长
    private float mCount=100;//分多少次运动
    private float mCurrent = 100;   // 当前已进行时长
    private float mPiece = mDuration/mCount; // 每一份的时长
    private int radarMargin = 40;

    public RadarMapView(Context context){
        this(context, null);
    }

    public RadarMapView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        contextX = w/2;
        contextY = h/2;
        radius = Math.min(w,h)/2*0.8f;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawPolygon(canvas);
        drawLigature(canvas);
        drawCover(canvas);
        drawTitle(canvas);
    }

    private void drawPolygon(Canvas canvas){
        Path path = new Path();
        float r=radius/(count-1);
        for(int i=1;i<count;i++){//绘制的层数
            curR=r*i;//当前层数半径
            path.reset();//使path重置
            for (int j=0;j<dataCount;j++){
                if(j==0){//第一次绘制，将起点移到第一个将要绘制的点,即多边形中心点移动到起点
                    path.moveTo(getPoint(j).x, getPoint(j).y);
                }else{
                    path.lineTo(getPoint(j).x, getPoint(j).y);
                }

            }
            path.close();
            canvas.drawPath(path,mainPaint);

        }
    }
    //五边形点的位置
    public Point getPoint(int position) {
        return getPoint(position, 0, 1);
    }
    public Point getPoint(int position, int radarMargin, float percent) {//以五边形的中心点为坐标原点
        int x = 0;
        int y = 0;
        switch (position) {
            case 0://第一象限，右上角顶点的坐标计算
                x = (int) (contextX + (curR + radarMargin) * Math.sin(angle) * percent);
                y = (int) (contextY - (curR + radarMargin) * Math.cos(angle) * percent);
                break;
            case 1://第四象限，右下角顶点的坐标计算
                x = (int) (contextX + (curR + radarMargin) * Math.sin(angle / 2) * percent);
                y = (int) (contextY + (curR + radarMargin) * Math.cos(angle / 2) * percent);
                break;
            case 2://第三象限，左下角顶点的坐标计算
                x = (int) (contextX - (curR + radarMargin) * Math.sin(angle / 2) * percent);
                y = (int) (contextY + (curR + radarMargin) * Math.cos(angle / 2) * percent);
                break;
            case 3://第二象限，左上角顶点的坐标计算
                x = (int) (contextX - (curR + radarMargin) * Math.sin(angle) * percent);
                y = (int) (contextY - (curR + radarMargin) * Math.cos(angle) * percent);
                break;
            case 4:// Y 轴正方向顶点的计算
                x = contextX;
                y = (int) (contextY - (curR + radarMargin) * percent);
                break;
        }
        return new Point(x, y);
    }


    public void drawLigature(Canvas canvas){
        Path path=new Path();
        for(int i = 0; i<dataCount; i++){
            path .reset();
            path.moveTo(contextX, contextY);
            path.lineTo(getPoint(i).x, getPoint(i).y);
            canvas.drawPath(path,mainPaint);
        }
    }

    public void drawCover(Canvas canvas){
        Path path = new Path();
        for (int i=0; i < dataCount; i++){
            float percent = (float) (values[i] / maxValue);
            float x = (float)(getPoint(i, 0, percent).x);
            float y = (float)(getPoint(i, 0, percent).y);
            if (i == 0) {
                path.moveTo(x, y);//将起点移到第一个将要绘制的点
            } else {
                path.lineTo(x, y);
            }
            valuePaint.setStyle(Paint.Style.FILL);
        }
        valuePaint.setAlpha(130);//设置画笔透明度
        valuePaint.setStyle(Paint.Style.FILL_AND_STROKE);//填充并且描边
        canvas.drawPath(path, valuePaint);
        mCurrent+=mPiece;
        if(mCurrent<mDuration) {
            for (int i=0;i<count;i++){
                temValuses[i]=values[i]*mCurrent/mDuration;
            }
            postInvalidateDelayed((long) mPiece);//刷新view
        }

    }

    private void drawTitle(Canvas canvas) {
        Paint.FontMetrics fontMetrics=textPaint.getFontMetrics();
        float fontHeight=fontMetrics.descent-fontMetrics.ascent;
        for (int i = 0; i < dataCount; i++) {
            int x = getPoint(i, radarMargin, 1).x;//获取添加 radarMargin 值之后的 X 坐标的指
            int y = getPoint(i, radarMargin, 1).y;//获取添加 radarMargin 值之后的 Y 坐标的指
            int titleWidth = (int) textPaint.measureText(titles[i]);
            switch (i) {
                case 1:
                    y += fontHeight / 2;
                    break;
                case 2:
                    x -= titleWidth;
                    y += fontHeight / 2;
                    break;
                case 3:
                    x -= titleWidth;
                    break;
                case 4:
                    x -= titleWidth / 2;
                    break;
            }
            canvas.drawText(titles[i], x, y, textPaint);
        }
    }


    public void setData(RadarMapData radarMapData) {
        this.radarMapData = radarMapData;
        count = radarMapData.getCount();
        angle = (float) (Math.PI * 2 / dataCount);
        mainPaint.setColor(radarMapData.getMainPaintColor());
//        titles=radarMapData.getTitles();
        radarMapData.setTextSize(100);
        textPaint.setTextSize(radarMapData.getTextSize());
//        values=radarMapData.getValuse();
    }

    private void init(){
        //雷达区
        mainPaint=new Paint();
        mainPaint.setColor(Color.BLUE);
        mainPaint.setStyle(Paint.Style.STROKE);
        mainPaint.setAntiAlias(true);
        //文字
        textPaint = new Paint();
        textPaint.setColor(Color.BLUE);
        textPaint.setStyle(Paint.Style.STROKE);
        textPaint.setAntiAlias(true);
        //数据
        valuePaint = new Paint();
        valuePaint.setColor(Color.BLUE);
        valuePaint.setStyle(Paint.Style.STROKE);
        valuePaint.setAntiAlias(true);
    }

    public void start(){
        mCurrent=0;
        postInvalidate();
    }
}
